function r60_writer(ouid, ou_ids, jpos, db_ids, rec) result(io)

    use iasing_osse
    use netcdf

    implicit none
    ! Arguments
    integer         :: ouid
    type(r4a_ids)   :: db_ids
    integer         :: jpos
    type(r4a_ids)   :: ou_ids
    type(r60_dat)   :: rec

    ! input/output status 
    integer :: io
    ! counters and markers for netcdf reading
    integer(JPI4), dimension(1) :: st1d, co1d
    integer(JPI4), dimension(2) :: st2d, co2d

    ! - setting markers
    st1d = jpos
    co1d = 1
    st2d = (/1,jpos/)
    
    io = 0
    
    ! writing dimension group
    
    if (db_ids%dim%gid .gt. 0) then
        if (db_ids%dim%atm .gt. 0) then
            io = nf90_put_var(ou_ids%dim%gid, ou_ids%dim%atm, rec%dim%nopro, &
                              start=st1d)
            if (io /= NF90_NOERR) then
                print*, "error writing 'natm' variable..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'natm' not written..."
        endif
        if (db_ids%dim%lev .gt. 0) then
            io = nf90_put_var(ou_ids%dim%gid, ou_ids%dim%lev, rec%dim%lev)
            if (io /= NF90_NOERR) then
                print*, "error writing 'plev' variable..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'plev' not written..."
        endif
        if (db_ids%dim%wia .gt. 0) then
            io = nf90_put_var(ou_ids%dim%gid, ou_ids%dim%wia, rec%dim%wia)
            if (io /= NF90_NOERR) then
                print*, "error writing 'wnia' variable..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'wnia' not written..."
        endif
        if (db_ids%dim%wng .gt. 0) then
            io = nf90_put_var(ou_ids%dim%gid, ou_ids%dim%wng, rec%dim%wng)
            if (io /= NF90_NOERR) then
                print*, "error writing 'wnng' variable..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'wnng' not written..."
        endif
        if (db_ids%dim%ocl .gt. 0) then
            io = nf90_put_var(ou_ids%dim%gid, ou_ids%dim%ocl, rec%dim%ocl)
            if (io /= NF90_NOERR) then
                print*, "error writing 'ochl' variable..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'ochl' not written..."
        endif
    else
        print*, "Dimensions group not written..."
    end if
    
    ! Writing Coordinates group
    if (db_ids%coo%gid .gt. 0) then
        if (db_ids%coo%lon .gt. 0) then
            io = nf90_put_var(ou_ids%coo%gid, ou_ids%coo%lon, rec%coo%lon, &
                              start=st1d)
            if (io /= NF90_NOERR) then
                print*, "error writing 'lon' variable..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'lon' not written..."
        endif
        if (db_ids%coo%lat .gt. 0) then
            io = nf90_put_var(ou_ids%coo%gid, ou_ids%coo%lat, rec%coo%lat, &
                              start=st1d)
            if (io /= NF90_NOERR) then
                print*, "error writing 'lat' variable..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'lat' not written..."
        endif
        if (db_ids%coo%ele .gt. 0) then
            io = nf90_put_var(ou_ids%coo%gid, ou_ids%coo%ele, rec%coo%ele, &
                              start=st1d)
            if (io /= NF90_NOERR) then
                print*, "error writing 'ele' variable..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'ele' not written..."
        endif
        if (db_ids%coo%dat .gt. 0) then
            io = nf90_put_var(ou_ids%coo%gid, ou_ids%coo%dat, rec%coo%dat, &
                              start=st1d)
            if (io /= NF90_NOERR) then
                print*, "error writing 'dat' variable..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'dat' not written..."
        endif
        if (db_ids%coo%tim .gt. 0) then
            io = nf90_put_var(ou_ids%coo%gid, ou_ids%coo%tim, rec%coo%tim, &
                              start=st1d)
            if (io /= NF90_NOERR) then
                print*, "error writing 'tim' variable..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'tim' not written..."
        endif
    else
        print*, "Coordinates group not written"
    endif

    ! Writing Observation parameters group
    if (db_ids%opa%gid .gt. 0) then
        if (db_ids%opa%psu .gt. 0) then
            io = nf90_put_var(ou_ids%opa%gid, ou_ids%opa%psu, rec%opa%psu, &
                              start=st1d)
            if (io /= NF90_NOERR) then
                print*, "error writing 'psu' variable..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'psu' not written..."
        endif
        if (db_ids%opa%tsu .gt. 0) then
            io = nf90_put_var(ou_ids%opa%gid, ou_ids%opa%tsu, rec%opa%tsu, &
                              start=st1d)
            if (io /= NF90_NOERR) then
                print*, "error writing 'tsu' variable..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'tsu' not written..."
        endif
        if (db_ids%opa%iza .gt. 0) then
            io = nf90_put_var(ou_ids%opa%gid, ou_ids%opa%iza, rec%opa%iza, &
                              start=st1d)
            if (io /= NF90_NOERR) then
                print*, "error writing 'iza' variable..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'iza' not written..."
        endif
        if (db_ids%opa%iaa .gt. 0) then
            io = nf90_put_var(ou_ids%opa%gid, ou_ids%opa%iaa, rec%opa%iaa, &
                              start=st1d)
            if (io /= NF90_NOERR) then
                print*, "error writing 'iaa' variable..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'iaa' not written..."
        endif
        if (db_ids%opa%sza .gt. 0) then
            io = nf90_put_var(ou_ids%opa%gid, ou_ids%opa%sza, rec%opa%sza, &
                              start=st1d)
            if (io /= NF90_NOERR) then
                print*, "error writing 'sza' variable..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'sza' not written..."
        endif
        if (db_ids%opa%saa .gt. 0) then
            io = nf90_put_var(ou_ids%opa%gid, ou_ids%opa%saa, rec%opa%saa, &
                              start=st1d)
            if (io /= NF90_NOERR) then
                print*, "error writing 'saa' variable..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'saa' not written..."
        endif
        if (db_ids%opa%lsm .gt. 0) then
            io = nf90_put_var(ou_ids%opa%gid, ou_ids%opa%lsm, rec%opa%lsm, &
                              start=st1d)
            if (io /= NF90_NOERR) then
                print*, "error writing 'lsm' variable..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'lsm' not written..."
        endif
        if (db_ids%opa%clc .gt. 0) then
            io = nf90_put_var(ou_ids%opa%gid, ou_ids%opa%clc, rec%opa%clc, &
                              start=st1d)
            if (io /= NF90_NOERR) then
                print*, "error writing 'clc' variable..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'clc' not written..."
        endif
        if (db_ids%opa%emi .gt. 0) then
            io = nf90_put_var(ou_ids%opa%gid, ou_ids%opa%emi, rec%opa%emi, &
                              start=st1d)
            if (io /= NF90_NOERR) then
                print*, "error writing 'emi' variable..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'emi' not written..."
        endif
        if (db_ids%opa%ert .gt. 0) then
            co2d = (/NIANG, 1/)
            io = nf90_put_var(ou_ids%opa%gid, ou_ids%opa%ert, rec%opa%ert, &
                              start=st2d, count=co2d)
            if (io /= NF90_NOERR) then
                print*, "error writing 'ert' variable..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'ert' not written..."
        endif
        if (db_ids%opa%rst .gt. 0) then
            io = nf90_put_var(ou_ids%opa%gid, ou_ids%opa%rst, rec%opa%rst, &
                              start=st1d)
            if (io /= NF90_NOERR) then
                print*, "error writing 'rst' variable..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'rst' not written..."
        endif
    else
        print*, "Observation parameters group not written"
    endif

    ! Writing Vertical profiles group
    if (db_ids%vpr%gid .gt. 0) then
        co2d = (/NLM60, 1/)
        if (db_ids%vpr%tem .gt. 0) then
            io = nf90_put_var(ou_ids%vpr%gid, ou_ids%vpr%tem, rec%vpr%tem, &
                              start=st2d, count=co2d)
            if (io /= NF90_NOERR) then
                print*, "error writing 'tem' variable..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'tem' not written..."
        endif
        if (db_ids%vpr%q .gt. 0) then
            io = nf90_put_var(ou_ids%vpr%gid, ou_ids%vpr%q, rec%vpr%q, &
                              start=st2d, count=co2d)
            if (io /= NF90_NOERR) then
                print*, "error writing 'q' variable..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'q' not written..."
        endif
        if (db_ids%vpr%co2 .gt. 0) then
            io = nf90_put_var(ou_ids%vpr%gid, ou_ids%vpr%co2, rec%vpr%co2, &
                              start=st2d, count=co2d)
            if (io /= NF90_NOERR) then
                print*, "error writing 'co2' variable..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'co2' not written..."
        endif
        if (db_ids%vpr%o3 .gt. 0) then
            io = nf90_put_var(ou_ids%vpr%gid, ou_ids%vpr%o3, rec%vpr%o3, &
                              start=st2d, count=co2d)
            if (io /= NF90_NOERR) then
                print*, "error writing 'o3' variable..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'o3' not written..."
        endif
        if (db_ids%vpr%co .gt. 0) then
            io = nf90_put_var(ou_ids%vpr%gid, ou_ids%vpr%co, rec%vpr%co, &
                              start=st2d, count=co2d)
            if (io /= NF90_NOERR) then
                print*, "error writing 'co' variable..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'co' not written..."
        endif
        if (db_ids%vpr%ch4 .gt. 0) then
            io = nf90_put_var(ou_ids%vpr%gid, ou_ids%vpr%ch4, rec%vpr%ch4, &
                              start=st2d, count=co2d)
            if (io /= NF90_NOERR) then
                print*, "error writing 'ch4' variable..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'ch4' not written..."
        endif
        if (db_ids%vpr%so2 .gt. 0) then
            io = nf90_put_var(ou_ids%vpr%gid, ou_ids%vpr%so2, rec%vpr%so2, &
                              start=st2d, count=co2d)
            if (io /= NF90_NOERR) then
                print*, "error writing 'so2' variable..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'so2' not written..."
        endif
    else
        print*, "Vertical profiles group not written"
    endif
    
    ! Writing Cloud information group
    if (db_ids%cld%gid .gt. 0) then
        co2d = (/NLM60, 1/)
        if (db_ids%cld%cc .gt. 0) then
            io = nf90_put_var(ou_ids%cld%gid, ou_ids%cld%cc, rec%cld%cc, &
                              start=st2d, count=co2d)
            if (io /= NF90_NOERR) then
                print*, "error writing 'cc' variable..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'cc' not written..."
        endif
        if (db_ids%cld%iwc .gt. 0) then
            io = nf90_put_var(ou_ids%cld%gid, ou_ids%cld%iwc, rec%cld%iwc, &
                              start=st2d, count=co2d)
            if (io /= NF90_NOERR) then
                print*, "error writing 'iwc' variable..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'iwc' not written..."
        endif
        if (db_ids%cld%lwc .gt. 0) then
            io = nf90_put_var(ou_ids%cld%gid, ou_ids%cld%lwc, rec%cld%lwc, &
                              start=st2d, count=co2d)
            if (io /= NF90_NOERR) then
                print*, "error writing 'lwc' variable..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'lwc' not written..."
        endif
        if (db_ids%cld%rwc .gt. 0) then
            io = nf90_put_var(ou_ids%cld%gid, ou_ids%cld%rwc, rec%cld%rwc, &
                              start=st2d, count=co2d)
            if (io /= NF90_NOERR) then
                print*, "error writing 'rwc' variable..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'rwc' not written..."
        endif
        if (db_ids%cld%swc .gt. 0) then
            io = nf90_put_var(ou_ids%cld%gid, ou_ids%cld%swc, rec%cld%swc, &
                              start=st2d, count=co2d)
            if (io /= NF90_NOERR) then
                print*, "error writing 'swc' variable..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'swc' not written..."
        endif
    else
        print*, "Cloud information group not written"
    endif
    
    

    ! Writing Radiances group
    if (db_ids%rbt%gid .gt. 0) then
        co2d = (/NIASI, 1/)
        if (db_ids%rbt%ria .gt. 0) then
            io = nf90_put_var(ou_ids%rbt%gid, ou_ids%rbt%ria, rec%rbt%ria, &
                              start=st2d, count=co2d)
            if (io /= NF90_NOERR) then
                print*, "error writing 'ria' variable..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'ria' not written..."
        endif
        co2d = (/NIANG, 1/)
        if (db_ids%rbt%rng .gt. 0) then
            io = nf90_put_var(ou_ids%rbt%gid, ou_ids%rbt%rng, rec%rbt%rng, &
                              start=st2d, count=co2d)
            if (io /= NF90_NOERR) then
                print*, "error writing 'rng' varngble..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'rng' not written..."
        endif
        if (db_ids%rbt%ngb .gt. 0) then
            io = nf90_put_var(ou_ids%rbt%gid, ou_ids%rbt%ngb, rec%rbt%ngb, &
                              start=st2d, count=co2d)
            if (io /= NF90_NOERR) then
                print*, "error writing 'ngb' varngble..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'ngb' not written..."
        endif
    else
        print*, "Radiances group not written"
    endif

    ! Writing Observations group
    if (db_ids%obs%gid .gt. 0) then
        co2d = (/NCHAN, 1/)
        if (db_ids%obs%oma .gt. 0) then
            io = nf90_put_var(ou_ids%obs%gid, ou_ids%obs%oma, rec%obs%oma, &
                              start=st1d)
            if (io /= NF90_NOERR) then
                print*, "error writing 'oma' variable..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'oma' not written..."
        endif
        if (db_ids%obs%obt .gt. 0) then
            io = nf90_put_var(ou_ids%obs%gid, ou_ids%obs%obt, rec%obs%obt, &
                              start=st2d, count=co2d)
            if (io /= NF90_NOERR) then
                print*, "error writing 'obt' variable..."
                print*, nf90_strerror(io)
                print*
                io = io+1
            endif
        else 
            print*, "Variable 'obt' not written..."
        endif
    
    else
        print*, "Observations group not written"
    endif


    return

end function r60_writer
